package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.DisassemblyAddInfo;
import com.sanyang.pda.bean.ManualPickingBean;
import com.sanyang.pda.bean.request.DisassemblyAddConfirmRequest;
import com.sanyang.pda.contract.AddPickingContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AddPickingPresenter extends BasePresenter<AddPickingContract.View> implements AddPickingContract.Presenter {

    @Override
    public void disassemblyAddInfo(String deliveryNo,String pallet,  String storagebinId,String barcode) {
        NetUtils.geApi().disassemblyAddInfo(deliveryNo, pallet, storagebinId, barcode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<DisassemblyAddInfo>>() {
                    @Override
                    public void accept(BaseAppEntity<DisassemblyAddInfo> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().setData(manualPickingBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void disassemblyAddConfirm(DisassemblyAddConfirmRequest disassemblyAddConfirmRequest) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(disassemblyAddConfirmRequest));
        NetUtils.geApi().disassemblyAddConfirm(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ManualPickingBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ManualPickingBean> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().showMessage(manualPickingBeanBaseAppEntity.getMsg());
                        getView().finishActivity();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void selectArticleInfoByBarcode(String barcode,String number) {
        NetUtils.geApi().selectArticleInfoByBarCodeAndNo(barcode,number)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ByBarcodeBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ByBarcodeBean>> listBaseAppEntity) throws Exception {
                        getView().setGoodsList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
